// index.js
// 获取应用实例
const app = getApp()
Page({
  data: {
    isFlag:true,
    num: 0,
    interval: "",
    isPlay: true,
    latitude: "",
    longitude: "",
    scale: 14,
    enable_traffic: true,
    show_compass: false,
    route: [],
    markers: [
      {
        iconPath: "https://wechats.oss-cn-beijing.aliyuncs.com/%E5%B0%8F%E8%BD%BF%E8%BD%A6%E5%B0%8F%E6%B1%BD%E8%BD%A6.png",
        id: 0,
        longitude: "116.434446",// 116.434446,39.90816
        latitude: "39.90816",
        height: 40,
        width: 40
      },
      {
        iconPath: "https://wechats.oss-cn-beijing.aliyuncs.com/begin.png",
        id: 1,
        longitude: "116.434446",
        latitude: "39.90816",
        height: 40,
        width: 40
      },
      {
        iconPath: "https://wechats.oss-cn-beijing.aliyuncs.com/location.png",
        id: 2,
        longitude: "116.395586",//{longitude: "116.395586", latitude: "39.907627"}中山公园 a=66
        latitude: "39.907627",
        height: 20,
        width: 20,
        label: {
          anchorY:-50,
          content: "中山公园",
          padding: 5,
          fontSize: 12,
          textAlign: 'center',
          display: 'ALWAYS',
          borderRadius: 5,
          borderWidth: 1,
          bgColor: '#ffffff',
          borderColor: "#c3c3c3"
        },
      },
      {
        iconPath: "https://wechats.oss-cn-beijing.aliyuncs.com/location.png",
        id: 3,
        longitude: "116.356136",//{longitude: "116.356136", latitude: "39.92761"} a=130
        latitude: "39.92761",
        height: 20,
        width: 20,
        label: {
          anchorY:-50,
          content: "顺城公园",
          padding: 5,
          fontSize: 12,
          textAlign: 'center',
          display: 'ALWAYS',
          borderRadius: 5,
          borderWidth: 1,
          bgColor: '#ffffff',
          borderColor: "#c3c3c3"
        },
      },
        {
          iconPath: "https://wechats.oss-cn-beijing.aliyuncs.com/location.png",
          id: 4,
          longitude: "116.382186",//德胜门{longitude: "116.382186", latitude: "39.948831"} a=190
          latitude: "39.948831",
          height: 20,
          width: 20,
          label: {
            anchorY:-50,
            content: "德胜门",
            padding: 5,
            fontSize: 12,
            textAlign: 'center',
            display: 'ALWAYS',
            borderRadius: 5,
            borderWidth: 1,
            bgColor: '#ffffff',
            borderColor: "#c3c3c3"
          },
      },
      {
        iconPath: "https://wechats.oss-cn-beijing.aliyuncs.com/location.png",
        id: 5,
        longitude: "116.418567",//{longitude: "116.418567", latitude: "39.94933"}雍和宫 a=213
        latitude: "39.94933",
        height: 20,
        width: 20,
        label: {
          anchorY:-40,
          content: "雍和宫",
          padding: 5,
          fontSize: 12,
          textAlign: 'center',
          display: 'ALWAYS',
          borderRadius: 5,
          borderWidth: 1,
          bgColor: '#ffffff',
          borderColor: "#c3c3c3"
        },
      },
      {
        iconPath: "https://wechats.oss-cn-beijing.aliyuncs.com/end.png",
        id: 6,
        longitude: "116.481028",//终点116.481028，39.9081 a= 350
        latitude: "39.989643",
        height: 40,
        width: 40
      }
    ],
    scene:[
        {
          longitude: "116.434446",
          latitude: "39.90816"
        },
        {
          longitude: "116.395586",//{longitude: "116.395586", latitude: "39.907627"}中山公园 a=66
          latitude: "39.907627"
          },
        {
          longitude: "116.356136",//{longitude: "116.356136", latitude: "39.92761"} a=130
          latitude: "39.92761"
        },
          {
            longitude: "116.382186",//德胜门{longitude: "116.382186", latitude: "39.948831"} a=190
            latitude: "39.948831"
        },
        {
          longitude: "116.418567",
          //{longitude: "116.418567", latitude: "39.94933"}雍和宫 
          latitude: "39.94933"
        },
        {
          longitude: "116.481028",
          //终点116.481028，39.9081 a= 350
          latitude: "39.989643",
        }
      ]
  },
  rad (d){
   return d *Math.PI/180.0//计算弧度
 },
 getDistance (lat1,lng1,lat2,lng2){
  var R = 6378137; // 地球半径，单位为米
var dLat = this.rad(lat2 - lat1);
var dLon = this.rad(lng2 - lng1);
var a = Math.sin (dLat / 2) * Math.sin (dLat / 2) +
            Math.cos (this.rad(lat1)) *Math.cos (this.rad(lat2))*
            Math.sin (dLon / 2) * Math.sin (dLon / 2);
var c = 2 * Math.atan2 (Math.sqrt (a), Math.sqrt (1 - a));
var d = R * c;
 return d;
},
  timer() {
    const that = this
    var a = that.data.num
    var interval = setInterval(function () {
      if (that.data.route.length <= a + 1||that.data.isFlag==false) {
        clearInterval(interval)
      }
      that.setData({
        ['markers[0].longitude']: that.data.route[a].longitude,
        //经纬度存储下来，在markers中
        ['markers[0].latitude']: that.data.route[a].latitude,
        longitude: that.data.route[a].longitude,
        latitude: that.data.route[a].latitude,
      })
     
     console.log(that.data.route[a])
     //console.log({longitude:that.data.route[a].longitude})
     //加入遍历
     for(var g=0;g<that.data.scene.length;g++){
      var dis=that.getDistance(that.data.scene[g].latitude,that.data.scene[g].longitude,that.data.route[a].latitude,that.data.route[a].longitude)
    console.log(dis)//实时计算全部的距离。
    //音频有些时候比较长，播放的时间比较长，不会影响其他景点的播报。
    //现在存在的问题是满足条件的就会重复播报。
      if(dis<=1000&&dis>900){
        this.audioCtx = wx.createAudioContext('greeting')
        this.audioCtx.setSrc('http://sfddr1eq5.hb-bkt.clouddn.com/image/e7f71398-df69-4dc1-9bed-cc0091c1d058.MP3')
        this.audioCtx.play();
        //加一个查看当前播放状态的判断，这样可以减少重复播报的问题
        // this.getBackgroundAudioPlayerState((state) => {
        //   console.log ('当前音频播放状态：', state);
        // }, (error) => {
        //   console.error ('获取播放状态失败：', error);
        // }, () => {
        //   console.log ('接口调用完成');
        // });
       }
      
    }
    that.setData({
      // 将路线的起点设置为地图中心点
      interval: interval,
    })
      a++;
    }, 1300)
  },
  onShow(e) {
    var _this = this
    _this.setData({
      // 将路线的起点设置为地图中心点
      isFlag:true,
    })
    _this.timer();
  },
  onLoad(options) {
    var _this = this
    var pointfor = []
    
    //通过wx.request发起HTTPS接口请求
    wx.request({
     //导航路线的绘制高德地图
      url: 'https://restapi.amap.com/v3/direction/driving?key=21571ff7adb0223185347d34893558f3&origin=116.434446,39.90816&destination=116.481028,39.989643&extensions=base&strategy=0&waypoints=116.357483,39.907234',
      success(res) {
        var route = res.data.route;
        var paths = route.paths;
        var steps = paths[0].steps; //选择第一个路径导航
        var pl = [];
        pl.push({
          longitude: "116.434446",
          latitude: "39.9081"
        })
        //将解压后的坐标放入点串数组pl中 绘制地图导航路线
        for (var i = 0; i < steps.length; i++) {
          var polylines = steps[i].polyline.toString().split(";");
          for (var j = 0; j < polylines.length; j++) {
            pl.push({
              longitude: polylines[j].toString().split(",")[0],
              latitude: polylines[j].toString().split(",")[1]
            })
          }
        };
        //最后一个地点
        pl.push({
          longitude: "116.481028",
          latitude: "39.989643"
        })
        for (var i = 0; i < pl.length; i++) {
          pointfor.push({
            longitude: pl[i].longitude,
            latitude: pl[i].latitude,
            id: i
          })
        }
        _this.setData({
          // 将路线的起点设置为地图中心点
          isFlag:true,
          longitude: "116.434446",
          latitude: "39.9081",
          route: pl,
          // 绘制路线
          polyline: [{
            points: pl,
            color: '#58c16c',
            width: 6,
            borderColor: '#2f693c',
            borderWidth: 1
          }],
        })
      }
    });
    wx.request({
      //查询定位
      url: 'https://restapi.amap.com/v3/ip?key=21571ff7adb0223185347d34893558f3',
      success(res) {
        console.log('res',res)
      }
    })
  },
 
  onHide(e){
    this.audioCtx = wx.createAudioContext('greeting')
    this.audioCtx.pause()
    var _this = this
    _this.setData({
      // 将路线的起点设置为地图中心点
      isFlag:false,
    })
  },
  onUnload(e){
    this.audioCtx = wx.createAudioContext('greeting')
    this.audioCtx.pause()
    this.setData({
      isFlag:false
    })
  },
  gotoPage:function () {
    wx.navigateTo({
      url: '../begin/begin', //要跳转到的页面路径
    })
  }
})